package club.beimeng.educenter.controller;


import club.beimeng.commonutils.JwtUtils;
import club.beimeng.commonutils.R;
import club.beimeng.educenter.entity.UcenterMember;
import club.beimeng.educenter.entity.vo.RegisterVo;
import club.beimeng.educenter.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author 小艾同学
 * @since 2021-08-02
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    // 手机登陆
    @PostMapping("loginMobile")
    public R loginUserMobile(@RequestBody UcenterMember member){
        // 调用service方法实现登陆
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }

    // 邮箱登陆
    @PostMapping("loginEmail")
    public R loginUserEmail(@RequestBody UcenterMember member){
        // 调用service方法实现登陆
        String token = memberService.loginEmail(member);
        return R.ok().data("token",token);
    }

    // 手机或邮箱注册的方法
    @ApiOperation("手机或邮箱注册")
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        Boolean answer = memberService.register(registerVo);
        return R.ok();
    }

    // 根据token 获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //使用工具类，获得用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //根据id查询数据库，获取
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userinfo",member);
    }
}

